package com.jyxd.web.controller.basic;

import com.alibaba.fastjson.JSON;
import com.jyxd.web.data.basic.Abp;
import com.jyxd.web.data.dto.AbpDTO;
import com.jyxd.web.data.dto.HttpResult;
import com.jyxd.web.data.user.User;
import com.jyxd.web.service.assessment.basic.AbpService;
import com.jyxd.web.util.DateUtil;
import com.jyxd.web.util.HttpCode;
import com.jyxd.web.util.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.jyxd.web.util.ExecuteUtil.errorIfEmpty;

@Api(tags = "ABP无创血压相关接口")
@Controller
@RequestMapping(value = "/abp")
public class AbpController {

    private static Logger logger = LoggerFactory.getLogger(AbpController.class);

    @Autowired
    private AbpService abpService;


    /**
     * 新增
     */
    @ApiOperation("有创血压添加修改")
    @RequestMapping(value = "/insertNew", method = RequestMethod.POST)
    @ResponseBody
    public String insertNew(@RequestBody AbpDTO abpNew, HttpSession session) {
        /**
         * map 中存在的  rowId   判断一组数据
         * 血糖的数组
         *
         */
        User user = (User) session.getAttribute("user");
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "操作失败");
        try {
            String patientId = abpNew.getPatientId();
            List<AbpDTO> list = abpNew.getList();
            if (list != null && list.size() > 0) {
                //修改
                for (AbpDTO abp : list) {
                    if (abp.getId() != null && !"".equals(abp.getId())) {
                        Abp abp1 = abpService.queryData(abp.getId());
                        abp1.setPatientId(patientId);
                        abp1.setDataTime(DateUtil.yyyyMMddHHmmssSdfToDate(abp.getDataTime()));
                        abp1.setCreateTime(new Date());
                        abp1.setOperatorCode(user.getUserName());
                        abp1.setSignature(abp.getSignature());
                        abp1.setStatus(1);
                        abp1.setContent(abp.getContent());
                        abp1.setRelevancyId(abp.getRelevancyId());
                        abpService.update(abp1);
                    } else {
                        if (abp.getSignature() != "" && abp.getSignature() != null && abp.getDataTime() != "" && abp.getDataTime() != null && abp.getContent() != "" && abp.getContent() != null) {
                            Abp abp1 = new Abp();
                            abp1.setId(UUIDUtil.getUUID());
                            abp1.setPatientId(patientId);
                            abp1.setSignature(abp.getSignature());
                            abp1.setDataTime(DateUtil.yyyyMMddHHmmssSdfToDate(abp.getDataTime()));
                            abp1.setCreateTime(new Date());
                            abp1.setOperatorCode(user.getUserName());
                            abp1.setStatus(1);
                            abp1.setRelevancyId(abp.getRelevancyId());
                            abp1.setContent(abp.getContent());
                            abpService.insert(abp1);
                        }
                    }
                }
            }
            json.put("code", HttpCode.OK_CODE.getCode());
            json.put("msg", "操作成功");

        } catch (Exception e) {
            logger.info("增加一条ABP记录失败:" + e);
        }
        return json.toString();
    }

    /**
     * 删除
     */
    @ApiOperation("通过id删除一条血压数据")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult deleteById(@RequestBody Map<String, Object> map) {
        try {
            errorIfEmpty(map, "参数不能为空");
            errorIfEmpty(map.get("id"), "删除数据id不能为空");
            Abp abp = abpService.queryData((String) map.get("id"));
            errorIfEmpty(abp, "需要删除数据不存在");
            abp.setStatus(-1);
            abpService.update(abp);
            return new HttpResult(200, "数据删除成功");
        } catch (Exception e) {
            return new HttpResult(500, "删除失败：" + e.getMessage());
        }
    }

    /**
     * 删除
     */
    @ApiOperation("血糖删除")
    @RequestMapping(value = "/deleteNew", method = RequestMethod.POST)
    @ResponseBody
    public String deleteNew(@RequestBody Map<String, Object> map, HttpSession session) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "删除失败");
        try {
            String rowId = map.get("rowId").toString();
            //通过rowid删除数据
            List<Abp> bloodSugars = abpService.queryListByRowId(rowId);
            for (Abp abp : bloodSugars) {
                abp.setStatus(-1);
                abpService.delete(abp);
            }
            json.put("code", HttpCode.OK_CODE.getCode());
            json.put("msg", "删除成功");

        } catch (Exception e) {
            logger.info("删除一条abp表记录:" + e);
        }
        return json.toString();
    }

    /**
     * 列表
     */
    @ApiOperation("获取abp的列表")
    @RequestMapping(value = "/queryDataNew", method = RequestMethod.POST)
    @ResponseBody
    public String queryDataNew(@RequestBody Map<String, Object> map, HttpSession session) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("msg", "查询列表失败");
        try {
            String patientId = map.get("patientId").toString();
            //通过rowid分组查询列表
            List<Abp> list = abpService.queryDataNew(patientId);
            String arr = JSON.toJSONString(list);
            json.put("data", arr);
            json.put("code", HttpCode.OK_CODE.getCode());
            json.put("msg", "查询列表成功");

        } catch (Exception e) {
            logger.info("查询列表abp表记录:" + e);
        }
        return json.toString();
    }

    @ApiOperation("通过rowId获取单个abp页面数据")
    @RequestMapping(value = "/queryRowDataNew", method = RequestMethod.POST)
    @ResponseBody
    public String queryRowDataNew(@RequestBody Map<String, Object> map, HttpSession session) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "查询数据失败");
        try {
            //通过rowid分组查询列表
            String rowId = map.get("rowId").toString();
            List<Abp> list = abpService.queryListByRowId(rowId);
            String arr = JSON.toJSONString(list);
            json.put("data", arr);
            json.put("code", HttpCode.OK_CODE.getCode());
            json.put("msg", "查询数据成功");

        } catch (Exception e) {
            logger.info("查询abp记录:" + e);
        }
        return json.toString();
    }

    @ApiOperation("ABP分页数据")
    @RequestMapping(value = "/queryListByPatientIdAndPageSize", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult queryListByPatientIdAndPageSize(@RequestBody Map<String, Object> map) {
        return abpService.queryListByPatientIdAndPageSize(map);
    }
}
